<?php
/**
 * 简历解析 Logic
 */

namespace app\common\logic;

use PhpOffice\PhpWord\IOFactory;
use think\File;

class ResumeParseLogic {

    /**
     * 依赖
     * yum install poppler
     * yum install poppler-utils
     *
     * pdf转html
     * @return string|false
     */
    static function pdfToHtml($pdf) {
        $tempDir = '/tmp/pdftohtml';
        if (!is_dir($tempDir)) {
            mkdir($tempDir, 0777, true);
        }
        $html = $tempDir . '/' . uniqid() . '.html';
        exec("pdftohtml {$pdf} {$html} -noframes -i", $output, $result_code); //-stdout 2>&1
        if ($result_code) {
            return false;
        }
        return @file_get_contents($html);
    }
    public static function pythonWordParseTime($file,$search_str='""'){
        exec("python /www/wwwroot/python/word.py {$file} {$search_str}", $output, $result_code); //-stdout 2>&1
        if($search_str && ($res = preg_grep("/{$search_str}/",$output))){
            return current($res);
        }else{
            return current($output);
        }

    }
    public static function pythonPdfParseTime($file,$search_str='""'){
        $html = self::pdfToHtml($file);
        $str = self::htmlTotext($html);
        return $str;
    }

    /**
     * word转html
     * @param $word
     * @return false|string
     */
    static function wordToHtml($word) {
        $tempDir = '/tmp/wordtohtml';
        if (!is_dir($tempDir)) {
            mkdir($tempDir, 0777, true);
        }
        try {
            $html = $tempDir . '/' . uniqid() . '.html';
            $phpWord = IOFactory::load($word);
            $write = IOFactory::createWriter($phpWord, 'HTML');
            $write->save($html);
            return file_get_contents($html);
        } catch (\Exception $e) {
//            var_dump($e->getTraceAsString());
            dolog('wordToHtmlError',[$e->getMessage(),$e->getTraceAsString()]);
            return false;
        }
    }

    /**
     * html转纯文本
     * @param $str
     * @return mixed
     */
    static function htmlTotext($str) {
        $str = preg_replace("/<style.*?<\\/style>/is", "", $str);
        $str = preg_replace("/<script.*?<\\/script>/is", "", $str);
        $str = preg_replace("/<br \\s*\\/>/i", "\n", $str);
        $str = preg_replace("/<\\/?p>/i", "\n", $str);
        $str = preg_replace("/<\\/?td>/i", "", $str);
        $str = preg_replace("/<\\/?div>/i", "\n", $str);
        $str = preg_replace("/<\\/?blockquote>/i", "", $str);
        $str = preg_replace("/<\\/?ul>/i", "\n", $str);
        $str = preg_replace("/<\\/?li>/i", " ", $str);
        $str = preg_replace("/<\\/?span>/i", " ", $str);
        $str = preg_replace("/<\\/?img>/i", "", $str);
        $str = preg_replace("/<\\/?br\/>/i", "\n", $str);
        $str = preg_replace("/&/i", "&", $str);
        $str = preg_replace("/&/i", "&", $str);
        $str = preg_replace("/</i", "<", $str);
        $str = preg_replace("/</i", "<", $str);
        $str = preg_replace("/“/i", '"', $str);
        $str = preg_replace("/&ldquo/i", '"', $str);
        $str = preg_replace("/‘/i", "'", $str);
        $str = preg_replace("/&lsquo/i", "'", $str);
        $str = preg_replace("/'/i", "'", $str);
        $str = preg_replace("/&rsquo/i", "'", $str);
        $str = preg_replace("/>/i", ">", $str);
        $str = preg_replace("/>/i", ">", $str);
        $str = preg_replace("/”/i", '"', $str);
        $str = preg_replace("/&rdquo/i", '"', $str);
        $str = preg_replace("/&nbsp;/i", " ", $str);
        $str = preg_replace("/&#160;/i", " ", $str);
        $str = preg_replace("/	/i", " ", $str);
        $str = preg_replace("/ +/", " ", $str); //连续空格去重
        $str = strip_tags($str);
        $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
        $str = preg_replace("/&#.*?;/i", "", $str);
        return $str;
    }

    //解析文件
    static function fileParse($file, $emailTitle = '') {
        try {
            //文件转html
            $fileType = strtolower(trim(substr(strrchr($file, '.'), 1)));
            if ($fileType == 'pdf') {
                $html = self::pdfToHtml($file);
            } elseif ($fileType == 'docx') {
                $html = self::wordToHtml($file);
            } elseif ($fileType == 'html') {
                $html = file_get_contents($file);
            } elseif ($fileType == 'xlsx') {
                return self::excelParse($file);
            }elseif (in_array($fileType,['zip','rar'])) {
                return self::excelParse($file);
            } else {
                return return_data(500,'文件后缀有误');
            }
            //html转文本
            $str = self::htmlTotext($html);
            $arr = explode("\n", $str);
            $arr = array_filter(array_map('trim', $arr)); //去除数组中的空元素
            $arr = array_values($arr);
//            dd(json_encode($arr,256));
            if(!isset($arr)) return_data(500,'文件信息为空');
//            exit;
            //判断平台
            if (in_array('智联App', $arr) || strpos($arr[1]??'', '点击此处联系候选人')) {
                $platform = 'zhilian';
             //elseif (isset($arr[6]) && strpos($arr[6], '最近工作') === 0 || strpos(end($arr), '操作时间：') === 0)
            } elseif ((isset($arr[7]) && $arr[7]=='仅供招聘专用，企业应尽保密义务，禁止外传') ||(isset($arr[count($arr)-2]) && $arr[count($arr)-2]=='一经发现我司有权采取一切必要措施，包括但不限于暂停或终止服务。')) {
                $platform = 'qiancheng';
            } elseif (strpos($arr[0], '来自58同城网的简历') === 0 ) {
                $platform = '58';
            } elseif (in_array('BOSS直聘', $arr) || strpos($emailTitle, '【BOSS直聘】')) {
                $platform = 'boss';
            } elseif(isset($arr[count($arr)-2]) && strpos($arr[count($arr)-2],'猎聘') !=''){
                $platform = 'liepin';
            }else {
                return [];
            }
            //解析数据
            $data = [];
            switch ($platform . '_' . $fileType) {
                case 'liepin_docx':
                    $data = self::liepinWordParse($arr);
                    break;
                case 'liepin_pdf':
                    $data = self::liepinPdfParse($arr);
                    break;
                case 'zhilian_pdf':
                    $data = self::zhiLianPdfParse($arr);
                    break;
                case 'zhilian_docx':
                    $serach_res = self::pythonWordParseTime($file,'简历下载于');
                    $serach_res && $arr[] = $serach_res;
                    $data = self::zhiLianWordParse($arr);
                    break;
                case 'qiancheng_pdf':
                    $data = self::qianchengPdfParse($arr);
                    break;
                case 'qiancheng_docx':
                    $data = self::qianchengWordParse($arr);
                    break;
                case '58_html':
                    $data = self::tongchengHtmlParse($arr);
                    break;
                case 'boss_html':
                    $data = self::bossHtmlParse($arr);
                    break;
                case 'boss_pdf':
                    $data = self::bossPdfParse($arr);
                    break;
                default:
                    return return_data(500,'文件来源不合法');;
            }
            if (empty($data) || !empty($data['code'])) {
                return $data;
            }
            return [$data];
        } catch (\Exception $e) {
//            print_r($e->getTraceAsString());
//            exit;
            $dir = SYS_UPLOAD_PATH.'error/'.date('Ymd').'/';
            if(!is_dir($dir)){
                mkdir($dir,0777,true);
            }
            $path_info = pathinfo($file);
            rename($file,$dir.$path_info['basename']);
            dolog(__METHOD__.':Error',[$e->getMessage(),$e->getFile(),$e->getLine()]);
            return return_data(500,'解析失败，请稍后再试');
        }

    }

    static function excelParse($file) {
        $excel = \PHPExcel_IOFactory::load($file);
        // 获取当前活动的工作表
        $sheet = $excel->getActiveSheet();
        // 将工作表的数据转换为数组
        $arr = $sheet->toArray(null, true, true, false);
        $keys = array_filter(array_shift($arr));
        foreach ($arr as $index => $row) {
            $arr[$index] = array_combine($keys, array_slice($row, 0, count($keys)));
        }
        $dataList = [];
        foreach ($arr as $row) {
            $birthday = '';
            if ($row['年龄']) {
                $age = $row['年龄'];
                $birthday = date('Y', strtotime("-$age years"));
            }
            $data = [
                'basic' => [
                    'fullname' => $row['姓名'],
                    'sex' => $row['性别'] == '男' ? 1 : 2,
                    'birthday' => $birthday,
                    'residence' => '',
                    'height' => '',
                    'marriage' => '',
                    'education' => $row['最高学历'],
                    'enter_job_time' => '',
                    'householdaddress' => '',
                    'specialty' => $row['自我描述'],
                    'addtime' => '',
                    'platform' => $row['渠道来源'],
                    'contact' => [
                        'mobile' => $row['手机'],
                        'email' => $row['邮箱']
                    ]
                ],
                'intention_list' => [],
                'education_list' => [],
                'work_list' => [],
                'language_list' => [],
                'certificate_list' => []
            ];

            if ($row['期望岗位'] || $row['期望城市']) {
                $data['intention_list'][] = [
                    'nature' => '',
                    'category' => $row['期望岗位'] ?? '',
                    'district' => $row['期望城市'] ?? '',
                    'minwage' => '',
                    'maxwage' => '',
                    'trade' => ''
                ];
            }

            $dataList[] = $data;
        }
        return $dataList;
    }



    //数据分组
    static function getGroupData($arr, $groupNames) {
        $groups = [];
        $indexArr = [];
        foreach ($groupNames as $index => $value) {
            if(stripos($value,'操作时间') !== false || stripos($value,'简历下载') !== false){
                $_arr_value = array_filter($arr, function ($v)use($value){
                    return stripos($v,$value) !== false;
                });
                $groups[$value] = preg_replace('/.*?(\d+).*?(\d+).*?(\d+).*/','$1-$2-$3',current($_arr_value));
                continue;
            }

            $start = array_search($value, $arr);
            if ($start === false) {
                if ($index == 0) {
                    $start = 1;
                } else {
                    continue;
                }
            } else {
                $start += 1;
            }
            $indexArr[] = [
                'name' => $value,
                'start' => $start
            ];
        }
        foreach ($indexArr as $index => $item) {
            $start = $item['start'];
            $end = isset($indexArr[$index + 1]['start']) ? ($indexArr[$index + 1]['start'] - 1) : count($arr);
            $groups[$item['name']] = array_slice($arr, $start, $end - $start);
        }
        return $groups;
    }


    //猎聘pdf解析
    static function liepinPdfParse($arr) {
        $groupNames = ['基本信息', '| 求职意向', '| 工作经历', '| 教育经历', '| 语言能力', '| 技能标签', '| 自我评价','| 附加信息','操作时间'];
        $groups = self::getGroupData($arr, $groupNames);

        $data = [
            'basic' => [
                'fullname' => '',
                'sex' => 0,
                'birthday' => '',
                'residence' => '',
                'height' => '',
                'marriage' => '',
                'education' => '',
                'enter_job_time' => '',
                'householdaddress' => '',
                'specialty' => '',
                'addtime' => '',
                'platform' => '猎聘',
                'contact' => [
                    'mobile' => '',
                    'email' => ''
                ],
                'download_time' => date('Y-m-d')
            ],
            'intention_list' => [],
            'education_list' => [],
            'work_list' => [],
            'language_list' => [],
            'certificate_list' => []
        ];

        //基本信息
        if (isset($groups['基本信息'])) {

            array_splice($groups['基本信息'],0,2);
            //手机号
            preg_match('/[0-9]{11}/', $groups['基本信息'][4], $matches);
            if (isset($matches[0])) {
                $data['basic']['contact']['mobile'] = $matches[0];
            }elseif($mobile_key = preg_grep('/手机/i',$groups['基本信息'])){// 模糊搜索数组元素中是否存在指定字符串
                preg_match('/[0-9]{11}/', current($mobile_key), $matches);
                $data['basic']['contact']['mobile'] = $matches[0];
            }

            if (empty($data['basic']['contact']['mobile'])) {
                return [];
            }

            //获取邮箱
            //preg_match('/^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?$/i', $groups['基本信息'][3], $matches);
            $data['basic']['contact']['email'] = strstr($groups['基本信息'][3], ' ');
            //姓名
            $data['basic']['fullname'] = strstr($groups['基本信息'][0], ' ', TRUE);
            if(stripos($groups['基本信息'][1],'|') !== false){
                $baseArr = explode('|', $groups['基本信息'][1]);
            }else{
                $baseArr = explode('|', $groups['基本信息'][2]);
            }
            $baseArr = array_map('trim', $baseArr);

            $data['basic']['sex'] = $baseArr[0] == '男' ? 1 : 2;

            preg_match('/[0-9]+/', $baseArr[1], $matches);
            $age = $matches[0];
            $data['basic']['birthday'] = date('Y', strtotime("-$age years"));

            $data['basic']['residence'] = $baseArr[2];

            preg_match('/[0-9]+/', $baseArr[4], $matches);
            if (isset($matches[0])) {
                if (strpos($baseArr[4], '年')) {
                    $year = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$year years"));
                } elseif (strpos($baseArr[4], '月')) {
                    $month = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$month month"));
                }
            }
            $data['basic']['education'] = trim($baseArr[4]);
        }

        //自我评价
        if (isset($groups['| 自我评价'])) {
            $data['basic']['specialty'] = implode("\n", $groups['| 自我评价']);
        }

        //求职意向
        if (isset($groups['| 求职意向'])) {
            $tmp = array_chunk($groups['| 求职意向'], 2);
            $intention_list = [];
            foreach ($tmp as $k=>$v){
                if(isset(explode('|',$v[0])[2])){
                    preg_match_all('/[0-9]+/i',explode('|',$v[0])[2],$matches);
                }
                $intention_list[] = [
                    'nature' => in_array('兼职',$groups['| 求职意向'])?'兼职':'全职',
                    'category' => isset(explode('|',$v[0])[0])?explode('|',$v[0])[0]:'',
                    'district' => isset(explode('|',$v[0])[1])?explode('|',$v[0])[1]:'',
                    'minwage' => isset($matches[0][0])?$matches[0][0]*1000:'',
                    'maxwage' => isset($matches[0][1])?$matches[0][1]*1000:'',
                    'trade' => isset($v[1])?$v[1]:''
                ];
            }
            $data['intention_list'] = $intention_list;
        }
        //教育经历
        if (isset($groups['| 教育经历'])) {
            $edu = array_chunk($groups['| 教育经历'], 2);
            $edu_list = [];
            if(count($edu)>=1){
                foreach ($edu as $k=>$v){
                    $tmp = explode('·',$v[0]);
                    $time = explode('-',$v[1]);
                    $edu_list[] = [
                        "starttime" =>isset($time[0])?$time[0]:'',
                        'endtime' =>isset($time[1])?$time[1]:'',
                        'school' =>isset(explode('  ',$tmp[0])[0])?explode('  ',$tmp[0])[0]:'',
                        'major' =>isset(explode('  ',$tmp[0])[1])?explode('  ',$tmp[0])[1]:'',
                        'education' =>(isset($tmp[1])&&isset($tmp[2]))?($tmp[1].'·'.$tmp[2]):''
                    ];
                }
            }

            $data['education_list'] = $edu_list;
        }

        //工作经历
        $workList = [];
        $starts = [];

        if(!empty($groups['| 工作经历'])){
            foreach ($groups['| 工作经历'] as $index => $value) {
                if (strpos($value, '职责业绩：') === 0) {
                    if ($index >= 3) {
                        $starts[] = $index;
                    } else {
                        $starts[] = 0;
                    }
                }
            }
            $companyname = [];
            $jobName = [];
            $datetime = [];
            $companyname[] = $groups['| 工作经历'][0];
            $jobName[] = $groups['| 工作经历'][2];
            $datetime[] = $groups['| 工作经历'][1];
            foreach ($starts as $index => $start) {
                $end = $starts[$index + 1] ?? count($groups['| 工作经历']);
                $info = implode(',',array_slice($groups['| 工作经历'], $start, ($end-9) - $start));
                $companyname[] = array_slice($groups['| 工作经历'], ($end-9), 1)[0];
                $jobName[] = array_slice($groups['| 工作经历'], ($end-7), 1)[0];
                $datetime[] = array_slice($groups['| 工作经历'], ($end-8), 1)[0];
                $workList[] = [
                    'starttime' => explode('-',$datetime[$index])[0],
                    'endtime' => isset(explode('-',$datetime[$index])[1])?explode('(',explode('-',$datetime[$index])[1])[0]:'',
                    //'todate' => $todate,
                    'companyname' =>$companyname[$index],
                    'jobname' => $jobName[$index],
                    'duty' => $info
                ];
            }
        }
        $data['work_list'] = $workList;

        //语言技巧
        if (isset($groups['| 语言能力'])){
            $data['language_list'][] =  [
                'language'=>$groups['| 语言能力'][0],
                'level'=>''
            ];
        }
        return $data;
    }
    //猎聘word解析
    static function liepinWordParse($arr) {
        $groupNames = ['基本信息', '|  求职意向', '|  工作经历', '|  教育经历', '|  语言能力', '|  技能标签', '|  自我评价','|  附加信息','操作时间'];
        $groups = self::getGroupData($arr, $groupNames);

        $data = [
            'basic' => [
                'fullname' => '',
                'sex' => 0,
                'birthday' => '',
                'residence' => '',
                'height' => '',
                'marriage' => '',
                'education' => '',
                'enter_job_time' => '',
                'householdaddress' => '',
                'specialty' => '',
                'addtime' => '',
                'platform' => '猎聘',
                'contact' => [
                    'mobile' => '',
                    'email' => ''
                ],
                'download_time' => !empty($groups['操作时间'])?$groups['操作时间']:''
            ],
            'intention_list' => [],
            'education_list' => [],
            'work_list' => [],
            'language_list' => [],
            'certificate_list' => []
        ];

        //基本信息
        if (isset($groups['基本信息'])) {
            //手机号
            preg_match('/[0-9]{11}/', $groups['基本信息'][4], $matches);
            if (isset($matches[0])) {
                $data['basic']['contact']['mobile'] = $matches[0];
            }
            if (empty($data['basic']['contact']['mobile'])) {
                return [];
            }
            //获取邮箱
            //preg_match('/^[a-z]([a-z0-9]*[-_]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?$/i', $groups['基本信息'][3], $matches);
            $data['basic']['contact']['email'] = strstr($groups['基本信息'][3], ' ');
            //姓名
            $data['basic']['fullname'] = strstr($groups['基本信息'][0], ' ', TRUE);
            $baseArr = explode('|', $groups['基本信息'][1]);
            $baseArr = array_map('trim', $baseArr);

            $data['basic']['sex'] = $baseArr[0] == '男' ? 1 : 2;

            preg_match('/[0-9]+/', $baseArr[1], $matches);
            $age = $matches[0];
            $data['basic']['birthday'] = date('Y', strtotime("-$age years"));

            $data['basic']['residence'] = $baseArr[2];

            preg_match('/[0-9]+/', $baseArr[4], $matches);
            if (isset($matches[0])) {
                if (strpos($baseArr[4], '年')) {
                    $year = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$year years"));
                } elseif (strpos($baseArr[4], '月')) {
                    $month = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$month month"));
                }
            }
            $data['basic']['education'] = trim($baseArr[4]);
        }

        //自我评价
        if (isset($groups['|  自我评价'])) {
            $data['basic']['specialty'] = implode("\n", $groups['|  自我评价']);
        }

        //求职意向
        if (isset($groups['|  求职意向'])) {
            $tmp = array_chunk($groups['|  求职意向'], 2);
            $intention_list = [];
            foreach ($tmp as $k=>$v){
                if(isset(explode('|',$v[0])[2])){
                    preg_match_all('/[0-9]+/i',explode('|',$v[0])[2],$matches);
                }
                $intention_list[] = [
                    'nature' => in_array('兼职',$groups['|  求职意向'])?'兼职':'全职',
                    'category' => isset(explode('|',$v[0])[0])?explode('|',$v[0])[0]:'',
                    'district' => isset(explode('|',$v[0])[1])?explode('|',$v[0])[1]:'',
                    'minwage' => isset($matches[0][0])?$matches[0][0]*1000:'',
                    'maxwage' => isset($matches[0][1])?$matches[0][1]*1000:'',
                    'trade' => isset($v[1])?$v[1]:''
                ];
            }
            $data['intention_list'] = $intention_list;
        }
        //教育经历
        if (isset($groups['|  教育经历'])) {
            $edu = array_chunk($groups['|  教育经历'], 2);
            $edu_list = [];
            if(count($edu)>=1){
                foreach ($edu as $k=>$v){
                    $tmp = explode('·',$v[0]);
                    $time = explode('-',$v[1]);
                    $edu_list[] = [
                        "starttime" =>isset($time[0])?$time[0]:'',
                        'endtime' =>isset($time[1])?$time[1]:'',
                        'school' =>isset(explode('  ',$tmp[0])[0])?explode('  ',$tmp[0])[0]:'',
                        'major' =>isset(explode('  ',$tmp[0])[1])?explode('  ',$tmp[0])[1]:'',
                        'education' =>(isset($tmp[1])&&isset($tmp[2]))?($tmp[1].'·'.$tmp[2]):''
                    ];
                }
            }

            $data['education_list'] = $edu_list;
        }


        //工作经历
        $workList = [];
        $starts = [];
        foreach ($groups['|  工作经历'] as $index => $value) {
            if (strpos($value, '职责：') === 0) {
                if ($index >= 3) {
                    $starts[] = $index;
                } else {
                    $starts[] = 0;
                }
            }
        }
        $companyname = [];
        $jobName = [];
        $datetime = [];
        $companyname[] = $groups['|  工作经历'][0];
        $jobName[] = $groups['|  工作经历'][2];
        $datetime[] = $groups['|  工作经历'][1];
        foreach ($starts as $index => $start) {
            $end = $starts[$index + 1] ?? count($groups['|  工作经历']);
            $info = implode(',',array_slice($groups['|  工作经历'], $start, ($end-9) - $start));
            $companyname[] = array_slice($groups['|  工作经历'], ($end-9), 1)[0];
            $jobName[] = array_slice($groups['|  工作经历'], ($end-7), 1)[0];
            $datetime[] = array_slice($groups['|  工作经历'], ($end-8), 1)[0];
            $workList[] = [
                 'starttime' => explode('-',$datetime[$index])[0],
                 'endtime' => explode('(',explode('-',$datetime[$index])[1])[0],
                 //'todate' => $todate,
                 'companyname' =>$companyname[$index],
                'jobname' => $jobName[$index],
                'duty' => $info
            ];

        }

        $data['work_list'] = $workList;

        //语言技巧
        if (isset($groups['|  语言能力'])){
           $data['language_list'][] =  [
               'language'=>$groups['|  语言能力'][0],
               'level'=>''
           ];
        }
        return $data;
    }

    //智联招聘pdf解析
    static function zhiLianPdfParse($arr) {
        //数据分组
        $groupNames = ['简历详情', '基本信息', '求职意向', '工作经历', '项目经历', '教育经历', '自我评价', '语言能力', '专业技能', '所获证书','简历下载'];
        $groups = self::getGroupData($arr, $groupNames);

        $str = str_replace('应聘职位：', '', $arr[9]);
        $position = explode('-',$str);
        $data = [
            'basic' => [
                'fullname' => '',
                'sex' => 0,
                'position'=>isset($position)?$position[0]:'',
                'birthday' => '',
                'residence' => '',
                'height' => '',
                'marriage' => '',
                'education' => '',
                'enter_job_time' => '',
                'householdaddress' => '',
                'specialty' => '',
                'addtime' => '',
                'platform' => '智联招聘',
                'contact' => [
                    'mobile' => '',
                    'email' => ''
                ],
                'download_time' => !empty($groups['简历下载'])?$groups['简历下载']:''
            ],
            'intention_list' => [],
            'education_list' => [],
            'work_list' => [],
            'language_list' => [],
            'certificate_list' => []
        ];

        //基本信息
        if (isset($groups['简历详情'])) {
            foreach ($groups['简历详情'] as $value) {
                if (substr($value, -strlen('点击此处联系候选人')) === '点击此处联系候选人') {
                    $data['basic']['fullname'] = trim(explode(' ', $value)[0]);
                }
                if (strpos($value, '手机') === 0) {
                    $data['basic']['contact']['mobile'] = trim(explode('：', $value)[1]);
                }
                if (strpos($value, '邮箱') === 0) {
                    $data['basic']['contact']['email'] = trim(explode('：', $value)[1]);
                }
                if (strpos($value, '简历下载于') === 0) {
                    preg_match('/：(.*)年(.*)月(.*)日/', $value, $matches);
                    $data['basic']['download_time'] = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
                }
            }

            if (empty($data['basic']['contact']['mobile'])) {
                return return_data(500,'无手机号');
            }

            $base = $groups['基本信息'][0];
            $base = explode('｜', $base);
            $base = array_map('trim', $base);
            $data['basic']['sex'] = $base[0] == '男' ? 1 : 2;

            preg_match_all('/[0-9]+/', $base[1], $matches);
            $data['basic']['birthday'] = $matches[0][1] . '-' . $matches[0][2];
            $data['basic']['education'] = $base[3];

            preg_match('/[0-9]+/', $base[2], $matches);
            if (isset($matches[0])) {
                if (strpos($base[2], '年')) {
                    $year = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$year years"));
                } elseif (strpos($base[2], '月')) {
                    $month = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$month month"));
                }
            }

            $address = $groups['基本信息'][1];
            $address = explode('｜', $address);
            $address = explode('：', $address[0]);
            $data['basic']['residence'] = $address[1];
        }

        //自我评价
        if (isset($groups['自我评价'])) {
            $specialty = implode("", $groups['自我评价']);
            $specialty = explode('*', trim($specialty, '*'));
            $specialty = implode("\n", $specialty);
            $data['basic']['specialty'] = trim($specialty);
        }
        if ($values = $groups['求职意向']??null) {
            $fuhao_shu = '｜';
            $jin_e_regexp = '/([0-9]+([.]{1}[0-9]+)?.*).*?-.*?([0-9]+([.]{1}[0-9]+)?.*).*?\//';
            $start_arr = [];
            $start_v = preg_grep('/'.$fuhao_shu.'/',$values);
            $start_arr[] = 0;
            foreach ($start_v as $k=>$v){
                $start_arr[] = $k + 1;
            }
            foreach ($start_arr as $k=>$v){
                if($k==0) continue;
                $end_arr[] = $v-1;
            }

            foreach ($start_arr as $key=>$value){
                if(!$category = $values[$value]??[]){
                    continue;
                }
                $first = explode(' ',$category);
                $category = $first[0]??'';
                $district = $first[1]??'';
                $info = explode($fuhao_shu,$values[$value + 1]);
                preg_match($jin_e_regexp, $info[0], $matches);
                $amount = function($data){
                    if(preg_match('/(\d+).*百/',$data,$match)){
                        return $match[1] * 100;
                    }else if(preg_match('/((\d+)([\.\d])*).*千/',$data,$match)){
                        return $match[1] * 1000;
                    }else if(preg_match('/((\d+)([\.\d])*).*万/',$data,$match)){
                        return $match[1] * 10000;
                    }
                };
                $minwage = call_user_func($amount,$matches[1]);
                $maxwage = call_user_func($amount,$matches[3]);
                $nature = end($info);
                $trade = trim($info[1]??'');

                $intention_list[] = [
                    'nature' => $nature??'',
                    'category' => $category??'',
                    'district' => $district??'',
                    'minwage' => $minwage??'',
                    'maxwage' => $maxwage??'',
                    'trade' => $trade =='不限行业'?'':$trade
                ];
            }

            $data['intention_list'] = $intention_list??[];
        }
//        if (isset($groups['求职意向'])) {
//            for ($i = 0; $i < count($groups['求职意向']); $i += 2) {
//                $job = $groups['求职意向'][$i];
//                $job = explode(' ', $job);
//                $category = trim($job[0]);
//                $district = trim($job[1]);
//
//                $wage = $groups['求职意向'][$i + 1];
//                $wageArr = explode('｜', $wage);
//                $wageRange = explode('-', explode('/', $wageArr[0])[0]);
//
//                preg_match('/[0-9]+([.]{1}[0-9]+){0,1}/', $wageRange[0], $matches);
//                $minwage = $matches[0] ?? 0;
//                if (strpos($wageRange[0], '千')) {
//                    $minwage = floatval($minwage) * 1000;
//                } else if (strpos($wageRange[0], '万')) {
//                    $minwage = floatval($minwage) * 10000;
//                }
//
//                preg_match('/[0-9]+([.]{1}[0-9]+){0,1}/', $wageRange[1], $matches);
//                $maxwage = $matches[0] ?? 0;
//                if (strpos($wageRange[1], '千')) {
//                    $maxwage = floatval($maxwage) * 1000;
//                } else if (strpos($wageRange[1], '万')) {
//                    $maxwage = floatval($maxwage) * 10000;
//                }
//
//                $nature = trim($wageArr[2]);
//                $trade = trim($wageArr[1]) == '不限行业' ? '' : trim($wageArr[1]);
//
//                $data['intention_list'][] = [
//                    'nature' => $nature,
//                    'category' => $category,
//                    'district' => $district,
//                    'minwage' => $minwage,
//                    'maxwage' => $maxwage,
//                    'trade' => $trade
//                ];
//                break; //只取第一个
//            }
//        }

        //教育经历
        if (isset($groups['教育经历'])) {
            for ($i = 0; $i < count($groups['教育经历']); $i += 2) {
                $eduInfo = $groups['教育经历'][$i];
                $eduInfo = explode(' ', $eduInfo);
                $school = $eduInfo[0];
                $major = $eduInfo[1];

                $eduTime = $groups['教育经历'][$i + 1];
                $eduTime = explode(' ', $eduTime);
                $startTime = str_replace('.', '-', $eduTime[0]);
                $endTime = str_replace('.', '-', $eduTime[2]);
                $education = $eduTime[3];

                $data['education_list'][] = [
                    "starttime" => $startTime,
                    'endtime' => $endTime,
                    'school' => $school,
                    'major' => $major,
                    'education' => $education
                ];
            }
        }

        //工作经历
        $workList = [];
        $starts = [];
        $values = $groups['工作经历'];
        foreach ($values as $index => $value) {
            if ($value == '工作描述') {
                if(!preg_match('/(\s+)|(有限公司)+/',$values[$index-2])){
                    $starts[] = $index-3;
                }else{
                    $starts[] = $index - 2;
                }

            }
        }
        foreach ($starts as $index => $start) {
            $end = $starts[$index + 1] ?? count($values);
            $info = array_slice($values, $start, $end - $start);

            $company = explode(' ', $info[0]);
            $regex_time_str = '/(\d{4}\.\d{2}).*-.*((\d{4}\.\d{2})|(至今)).*/';
            $slice_start = 3;
            if(!preg_match($regex_time_str,$info[1],$matches)){
                preg_match($regex_time_str,$info[2],$matches);
                $slice_start = 4;
            }
            $startTime = $matches[1]??'';
            $endTime = $matches[2]??'';


//            $time = explode(' ', $info[1]);
            $duty = implode('', array_slice($info, $slice_start, count($info) - $slice_start - 1));

//            $startTime = str_replace('.', '-', $time[4] ?? $time[0]);
//            $endTime = str_replace('.', '-', $time[6] ?? $time[2]);
//            var_dump($info);
            $todate = 0;
            if ($endTime == '至今') {
                $endTime = '';
                $todate = 1;
            }
            $workList[] = [
                'starttime' => $startTime,
                'endtime' => $endTime,
                'todate' => $todate,
                'companyname' => $company[0],
                'jobname' => $company[1],
                'duty' => $duty
            ];
        }
        $data['work_list'] = $workList;

        //语言
        if (isset($groups['语言能力'])) {
            $languageList = [];
            $langCount = count($groups['语言能力']);
            for ($i = 0; $i < $langCount; $i += 5) {
                $languageList[] = [
                    'language' => $groups['语言能力'][$i],
                    'level' => $groups['语言能力'][$i + 2]
                ];
            }
            $data['language_list'] = $languageList;
        }
        // 所获证书
        if($values = $groups['所获证书']??[]){
            $certificate_list = [];
            $regex_time_str = '/(\d{4}).*?(\d{2}).*?/';
            foreach ($values as $k=>$value){
                if(preg_match($regex_time_str,$value,$matches)){
                    $certificate_list[] = [
                        'name' => $values[$k-1],
                        'obtaintime' => $matches[1].'-'.$matches[2]
                    ];
                }
            }
            $data['certificate_list'] = $certificate_list;
        }
       // print_r($data);die;
        return $data;
    }

    //智联word解析
    static function zhiLianWordParse($arr) {

        //数据分组
        $groupNames = ['简历详情', '基本信息', '求职意向', '工作经历', '项目经历', '教育经历', '自我评价', '语言能力', '专业技能','简历下载'];
        $groups = self::getGroupData($arr, $groupNames);

        $data = [
            'basic' => [
                'fullname' => '',
                'sex' => 0,
                'birthday' => '',
                'residence' => '',
                'height' => '',
                'marriage' => '',
                'education' => '',
                'enter_job_time' => '',
                'householdaddress' => '',
                'specialty' => '',
                'addtime' => '',
                'platform' => '智联招聘',
                'contact' => [
                    'mobile' => '',
                    'email' => ''
                ],
                'download_time' => !empty($groups['简历下载'])?$groups['简历下载']:''
            ],
            'intention_list' => [],
            'education_list' => [],
            'work_list' => [],
            'language_list' => [],
            'certificate_list' => []
        ];

        //手机号
        if (isset($groups['简历详情'])) {
            foreach ($groups['简历详情'] as $item) {
                preg_match('/[0-9]{11}/', $item, $matches);
                if (isset($matches[0])) {
                    $data['basic']['contact']['mobile'] = $matches[0];
                }
            }
            if (empty($data['basic']['contact']['mobile'])) {
                return [];
            }

            $data['basic']['fullname'] = explode(' ', $groups['简历详情'][0])[0];
            $baseArr = explode('｜', $groups['基本信息'][0]);
            $baseArr = array_map('trim', $baseArr);
            $data['basic']['sex'] = $baseArr[0] == '男' ? 1 : 2;

            preg_match_all('/[0-9]+/', $baseArr[1], $matches);
            $data['basic']['birthday'] = $matches[0][1] . '-' . $matches[0][2];

            $baseArr2 = explode('｜ ', $groups['基本信息'][1]);
            $data['basic']['residence'] = explode('：', $baseArr2[0])[1];

            $data['basic']['education'] = $baseArr[3];

            preg_match('/[0-9]+/', $baseArr[2], $matches);
            if (isset($matches[0])) {
                if (strpos($baseArr[2], '年')) {
                    $year = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$year years"));
                } elseif (strpos($baseArr[2], '月')) {
                    $month = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$month month"));
                }
            }
        }

        //自我评价
        if (isset($groups['自我评价'])) {
            $specialty = $groups['自我评价'][0];
            $specialty = explode('*', trim($specialty, '*'));
            $specialty = implode("\n", $specialty);
            $data['basic']['specialty'] = trim($specialty);
        }

        if ($values = $groups['求职意向']??null) {
            $fuhao_shu = '｜';
            $jin_e_regexp = '/([0-9]+([.]{1}[0-9]+)?.*).*?-.*?([0-9]+([.]{1}[0-9]+)?.*).*?\//';
            $start_arr = [];
            $start_v = preg_grep('/'.$fuhao_shu.'/',$values);
            $start_arr[] = 0;
            foreach ($start_v as $k=>$v){
                $start_arr[] = $k + 1;
            }
            foreach ($start_arr as $k=>$v){
                if($k==0) continue;
                $end_arr[] = $v-1;
            }

            foreach ($start_arr as $key=>$value){
                if(!$category = $values[$value]??[]){
                    continue;
                }
                $first = explode(' ',$category);
                $category = $first[0]??'';
                $district = $first[1]??'';
                $info = explode($fuhao_shu,$values[$value + 1]);
                preg_match($jin_e_regexp, $info[0], $matches);
                $amount = function($data){
                    if(preg_match('/(\d+).*百/',$data,$match)){
                        return $match[1] * 100;
                    }else if(preg_match('/((\d+)([\.\d])*).*千/',$data,$match)){
                        return $match[1] * 1000;
                    }else if(preg_match('/((\d+)([\.\d])*).*万/',$data,$match)){
                        return $match[1] * 10000;
                    }
                };
                $minwage = call_user_func($amount,$matches[1]);
                $maxwage = call_user_func($amount,$matches[3]);
                $nature = end($info);
                $trade = trim($info[1]??'');

                $intention_list[] = [
                    'nature' => $nature??'',
                    'category' => $category??'',
                    'district' => $district??'',
                    'minwage' => $minwage??'',
                    'maxwage' => $maxwage??'',
                    'trade' => $trade =='不限行业'?'':$trade
                ];
            }

            $data['intention_list'] = $intention_list??[];
        }

        //教育经历
        if (isset($groups['教育经历'])) {
            $eduInfo = $groups['教育经历'][0];
            $eduInfo = explode(' ', $eduInfo);
            $school = $eduInfo[0];
            $major = $eduInfo[1];

            $eduTime = $groups['教育经历'][1];
            $eduTime = explode(' ', $eduTime);
            $startTime = str_replace('.', ' - ', $eduTime[0]);
            $endTime = str_replace('.', ' - ', $eduTime[2]);
            $education = $eduTime[3];

            $data['education_list'] = [
                [
                    "starttime" => $startTime,
                    'endtime' => $endTime,
                    'school' => $school,
                    'major' => $major,
                    'education' => $education
                ]
            ];
        }

        //工作经历
        $workList = [];
        $starts = [];
        foreach ($groups['工作经历'] as $index => $value) {
            if ($value == '工作描述 ：') {
                $starts[] = $index - 2;
            }
        }
        foreach ($starts as $index => $start) {
            $end = $starts[$index + 1] ?? count($groups['工作经历']);
            $info = array_slice($groups['工作经历'], $start, $end - $start);
            $company = explode(' ', $info[0]);
            $time_arr = explode(' ', $info[1]);
            $time = count($time_arr) <=1 ?$info[1]:explode(' ', $info[1])[1];

            preg_match('/(.*)-(.*)\(/', $time, $matches);
            $startTime = str_replace('.', '-', $matches[1]);
            $endTime = str_replace('.', '-', $matches[2]);
            $todate = 0;
            if ($endTime == '至今') {
                $endTime = '';
                $todate = 1;
            }

            $duty = end($info);
            $workList[] = [
                'starttime' => $startTime,
                'endtime' => $endTime,
                'todate' => $todate,
                'companyname' => $company[0],
                'jobname' => $company[1],
                'duty' => $duty
            ];
        }
        $data['work_list'] = $workList;

        //语言
        if (isset($groups['语言能力'])) {
            $values = $groups['语言能力'];
            foreach ($values as $key=>$value){
                if(stripos($value,'简历下载于') !== false){
                    unset($values[$key]);
                }
            }
            $languageList = [];
            $langCount = count($values);
            for ($i = 0; $i < $langCount; $i += 5) {
                $languageList[] = [
                    'language' => $values[$i],
                    'level' => $values[$i + 2]
                ];
            }
            $data['language_list'] = $languageList;
        }

        return $data;
    }

    /**
     * 前程无忧pdf数组处理 --- 去除水印数据
     * @param $data
     * @return array
     */
    public static function qianchengPdfExtraHandleData($data){
        $company = explode('：',current(preg_grep('/应聘公司/',$data)))[1]??'';
        if(!$company){
            $company_arr = preg_grep('/以上人才信息仅供(.*?)招聘使用/',$data);
            preg_match('/以上人才信息仅供(.*?)招聘使用/',current($company_arr),$match);

            $company = $match[1]??'';
        }
        $keys = array_keys(preg_grep('/活跃时间/',$data));
        $begin_index = $keys?max($keys):0;

        $data = array_slice($data,$begin_index);

        $id = current(preg_grep('/^ID：\d+\s+\d+/',$data));
        $ID = preg_replace('/ID：(\d+).*?(\d+).*/','$1',$id);

        $invalid_ids = 0;
        foreach ($data as $value){
            if(preg_match('/((ID：)|('.$company.')).*?(\d{6,})/',$value,$match)){
                if(!empty($match[4]) && $match[4] != $ID)
                $invalid_ids = $match[4]??'';
            }
        }

        $_arr_value = array_filter($data, function ($v){
            return stripos($v,'操作时间') !== false;
        });
        $handle_time = preg_replace('/.*?(\d+).*?(\d+).*?(\d+).*/','$1-$2-$3',current($_arr_value));
//        foreach ($data as $key=>$value){
//            if( preg_match('/^[\d\s-]+$/',trim($value)) && stripos($data[$key+1],'/月') !==false ){
//                $data[$key] = $value.$data[$key+1];
//                unset($data[$key+1]);
//            }
//        }
//        var_export($data);
        $result = array_filter($data,function($v)use($company,$ID,$invalid_ids,$handle_time){
            if(preg_match('/('.$ID.'\s+\d+.*?-.*?\d+.*?(-.*?\d+)?).*?([^\d-]+)/',$v,$match)){// 过滤ID + 时间 + 有用的信息
//                var_export([$v,$match]);
                $v = trim($match[3]??'');
            }
            if(preg_match('/ID：([^\d-]+)/',$v,$match)){// ID：+有用的信息
                $v = trim($match[1]??'');
            }
            // 获取ID：里面的信息
            if(preg_match('/ID：('.$ID.').*?(\D+\s\D+)/',$v,$match)){
                $v = trim($match[2]??'');
            }
            if(preg_match('/(\D+).*?ID：('.$ID.').*/',$v,$match)){
                $v = trim($match[1]??'');
            }
            if(preg_match("/$company\s+(.*)/",$v,$match)){
                if(!preg_match('/：\d+/',$v)){
                    $v = trim($match[1]??'');
                }
            }


            // 处理公司名称的水印
            $com_str_bool = false;
            if(stripos($v,'｜') === false){
                $com_str_arr = explode(' ',$v);
                $com_str_bool = stripos($company,$com_str_arr[0]) !== false || (!empty($com_str_arr[1]) && stripos('有限公司',$com_str_arr[0]) !== false);
            }

            return (!$company||$company !== $v) && (!$ID||(!preg_match('/'.$ID.'/',$v))||preg_match('/操作时间/',$v)) &&
                !preg_match('/^ID/',$v) && !preg_match('/仅供招聘专用/',$v) &&
                stripos($company,$v) ===false && (mb_strlen($v) > 1 || is_string($v)) &&
                !(preg_match('/^[\d\s-]+$/',trim($v)) && stripos($v,$invalid_ids) !== false && mb_strlen($v)<10) && //出现金额单独一行的需要stripos($v,$invalid_ids)
                stripos($handle_time,$v) === false && // 过滤掉操作时间格式数据
                stripos($ID,$v) === false &&// 过滤掉部分ID
                stripos($v,$invalid_ids) === false &&
                stripos($v,$handle_time) === false && !$com_str_bool;
        });

        $res = [];
        foreach ($result as $key=>$value){
            if(preg_match('/('.$ID.'\s+\d+.*?-.*?\d+.*?(-.*?\d+)?).*?([^\d-]+)/',$value,$match)){// 过滤ID + 时间 + 有用的信息
                $value = trim($match[3]??'');
            }
            if(preg_match('/ID：([^\d-]+)/',$value,$match)){// ID：+有用的信息
                $value = trim($match[1]??'');
            }
            if(preg_match('/ID：('.$ID.').*?(\D+\s\D+)/',$value,$match)){
                $value = trim($match[2]??'');
            }
            if(preg_match('/(\D+).*?ID：('.$ID.').*/',$value,$match)){
                $value = trim($match[1]??'');
            }
            if(preg_match('/(\D+).*?：.*?(\d+).*/',$value,$match)){ // 过滤公司名称加 招聘平台系统的Id号
                if(stripos($company,$match[1]) !== false){
                    continue;
                }
            }
            if(preg_match("/$company\s+(.*)/",$value,$match)){
                if(!preg_match('/：\d+/',$value)){ // 包含无用的数字
                    $value = trim($match[1]??'');
                }

            }
            $value = str_replace($company,'',$value);
            if(stripos($value,'操作时间') !== false){
                $value = '操作时间：'.$handle_time;
            }
            $res[] = $value;
        }
        return $res;
    }

    /**
     *前程无忧 处理数据库数据
     * @param $groups
     * @return array
     */
    public static function qianchengPdfHandleData($groups){
        $data = [
            'basic' => [
                'fullname' => '',
                'sex' => 0,
                'birthday' => '',
                'residence' => '',
                'height' => '',
                'marriage' => '',
                'education' => '',
                'enter_job_time' => '',
                'householdaddress' => '',
                'specialty' => '',
                'addtime' => '',
                'platform' => '前程无忧',
                'contact' => [
                    'mobile' => '',
                    'email' => ''
                ],
                'download_time' => !empty($groups['操作时间'])?$groups['操作时间']:''
            ],
            'intention_list' => [],
            'education_list' => [],
            'work_list' => [],
            'language_list' => [],
            'certificate_list' => []
        ];

        //基本信息
        if (isset($groups['基本信息'])) {
            $values = $groups['基本信息'];
            //手机号
            foreach ($values as $item) {
                preg_match('/[0-9]{11}/', $item, $matches);
                if (isset($matches[0])) {
                    $data['basic']['contact']['mobile'] = $matches[0];
                }
            }
            if (empty($data['basic']['contact']['mobile'])) {
                return return_data(500,'手机号为空');
            }

            $data['basic']['fullname'] = $values[0];
            $baseArr = explode('｜', $values[2]);
            $baseArr = array_map('trim', $baseArr);

            $data['basic']['sex'] = $baseArr[0] == '男' ? 1 : 2;

            preg_match('/[0-9]+/', $baseArr[1], $matches);
            $age = $matches[0];
            $data['basic']['birthday'] = date('Y', strtotime("-$age years"));

            $data['basic']['residence'] = $baseArr[2];

            preg_match('/[0-9]+/', $baseArr[3], $matches);
            if (isset($matches[0])) {
                if (strpos($baseArr[3], '年')) {
                    $year = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$year years"));
                } elseif (strpos($baseArr[3], '月')) {
                    $month = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$month month"));
                }
            }

            $eduIndex = array_search('最高学历/学位', $values);
            $eduArr = explode('｜', $values[$eduIndex + 1]);
            if(!empty($eduArr[2])){
                $data['basic']['education'] = trim($eduArr[2]);
            }elseif(!empty($eduArr[1])){
                $data['basic']['education'] = trim($eduArr[1]);
            }else{
                $data['basic']['education'] = trim($eduArr[0]);
            }

        }

        //自我评价
        if (isset($groups['个人优势'])) {
            $data['basic']['specialty'] = implode("\n", $groups['个人优势']);
        }

        //求职意向
        if ($values = $groups['求职意向']??null) {
            $fuhao_shu = '｜';
            $start_arr = [];
            $start_v = preg_grep('/'.$fuhao_shu.'/',$values);
            $start_arr[] = 0;
            $regexp_amount_str = '/([0-9]+([.]{1}[0-9]+){0,1}).*-.*([0-9]+([.]{1}[0-9]+){0,1})/';
            foreach ($start_v as $k=>$v){
                if(!empty($values[$k + 1]) && preg_match($regexp_amount_str,$values[$k + 1])){
                    $start_arr[] = $k + 1;
                }elseif(!empty($values[$k + 2]) && preg_match($regexp_amount_str,$values[$k + 2])){
                    $start_arr[] = $k + 2;
                }elseif(!empty($values[$k + 3])){
                    $start_arr[] = $k + 3;
                }

            }
            foreach ($start_arr as $k=>$v){
                if($k==0) continue;
                $end_arr[] = $v-1;
            }
//            var_export([$values,$start_arr]);
            foreach ($start_arr as $key=>$value){
                if(!$category = $values[$value]??[]){
                    continue;
                }
                $trade = '';
                if($key != 0){
                    $job = explode(' ', $values[$value]);
                    $category = trim($job[0]);
                    $job_key = current(array_keys(preg_grep('/[0-9]+([.]{0,1}[0-9]+){0,1}/',$job)));

                    if(empty($job[$job_key]) || !preg_match_all('/[0-9]+([.]{0,1}[0-9]+){0,1}/', $job[$job_key], $matches)){
                        continue;
                    }
                    $minwage = $matches[0][0]??0;
                    $maxwage = $matches[0][1]??0;

                    $district = $values[$value+1];
                    if(stripos($district,$fuhao_shu) === false){
                        $district = $values[$value+2]??'';
                    }

                    $info = explode($fuhao_shu,$district);
                    $district = $info[0];
                    $nature_key = current(array_keys(preg_grep('/(全职)|(兼职)|(实习)/',$info)));
                    $nature = trim($info[$nature_key]);
                    $trade = trim($info[$nature_key + 1]??'');

                }else{
                    $res_match = preg_match_all('/[0-9]+([.]{0,1}[0-9]+){0,1}/', $values[1], $matches);
                    if($res_match){
                        $minwage = $matches[0][0]??0;
                        $maxwage = $matches[0][1]??0;
                        if(stripos($values[2],$fuhao_shu) !== false){
                            $info = explode($fuhao_shu,$values[2]);

                            $district = trim($info[0]);
                            $nature_key = current(array_keys(preg_grep('/(全职)|(兼职)|(实习)/',$info)));
                            $nature = trim($info[$nature_key]);
                            $trade = trim($info[$nature_key + 1]??'');
                        }else{
                            $district = str_replace('-', '/', ($values[2]));
                            if(stripos($values[3],$fuhao_shu) !== false){
                                if(preg_match("/{$fuhao_shu}?\s*(.*?)\s*{$fuhao_shu}?\s*(.*)\s*/",$values[3],$matches)){
                                    $nature = $matches[1]??'';
                                    $trade = $matches[2]??'';
                                }elseif(preg_match("/(.*)\s*{$fuhao_shu}?\s*(.*)\s*/",$values[3],$matches)){
                                    $nature = $matches[2]??'';
                                }

                            }else{
                                $nature = (str_replace(''.$fuhao_shu.'', '', $values[3]));
                            }
                        }
                    }else{ // 数据在第一行 简历求职意向数据异常的 期望薪资没有 或者职位没有的
                        $res_match = preg_match_all('/[0-9]+(\.?[0-9]+){0,1}/', $category, $matches);
                        if($res_match){
                            preg_match('/(\D+)?\s+\d+.*/',$category,$match);

                            $category = $match[1]??'';

                            // 找到薪资的及以上或及以下数据
                            $range_pay_str = preg_grep('/及以/',$values);
                            if($range_pay_str){
                                $range_pay_key = current(array_keys($range_pay_str));
                                $minwage = 0;
                                $maxwage = $matches[0][0]??0;
                            }else{
                                $range_pay_key = 0;
                                $minwage = $matches[0][0];
                                $maxwage = $matches[0][1];
                            }

                            $info = explode($fuhao_shu,$values[$range_pay_key+1]);
                        }else{
                            $info = explode($fuhao_shu,$values[1]);
                        }
                        $district = trim($info[0]);
                        $nature_key = current(array_keys(preg_grep('/(全职)|(兼职)|(实习)/',$info)));
                        $nature = trim($info[$nature_key]);
                        $trade = trim($info[$nature_key + 1]??'');
                    }
                }

                $intention_list[] = [
                    'nature' => $nature??'',
                    'category' => $category??'',
                    'district' => $district??'',
                    'minwage' => $minwage??'',
                    'maxwage' => $maxwage??'',
                    'trade' => $trade??''
                ];
            }

            $data['intention_list'] = $intention_list??[];
        }

        //教育经历
        if ($values = $groups['教育经历']??[]) {
            $schoole_name_regexp = '/(中学)|(学院)|(大学)|(学校)|(成人教育)/';
            // 处理 非学校名称及专业的数据
            foreach($values as $k=>$value){
                if(preg_match('/(\d{4}\.\d{2}).*-.*(\d{4}\.\d{2}).*/',$value)){
                    if(($school_name = $values[$k + 1]??'') && preg_match($schoole_name_regexp,$school_name)){// 学校的名称跑到时间的下面
                        $school_name = $values[$k + 1];
                        $values[$k] = $school_name;
                        $values[$k + 1] = $value;
                    }else{
                        if(($school_name = $values[$k - 1]??'') && !preg_match($schoole_name_regexp,$school_name) && $k - 1 > 0){
                            unset($values[$k - 1]);
                        }
                    }
                }
            }
            $values = array_values($values);
            $start_v = preg_grep('/(\d{4}\.\d{2}).*-.*(\d{4}\.\d{2}).*/',$values);
            $start_arr = [];
            $end_arr = [];
            foreach ($start_v as $k=>$v){
                if(!preg_match($schoole_name_regexp,$values[$k - 1]) && $k-1 > 0){
                    continue;
                }
                $start_arr[] = $k;
            }
            foreach ($start_arr as $k=>$v){
                if($k==0) continue;
                $end_arr[] = $v-2;
            }
            foreach ($start_arr as $key=>$value){
                $school = $values[$value - 1];
                if(preg_match('/(.*?)\s+.*/',$school,$match)){
                    $school = $match[1]??$school;
                }
                $eduArr = explode('·', $values[$value + 1]);
                $education = trim($eduArr[0]);
                if (count($eduArr) <= 1) {
                    if(!empty($values[$value + 2])){
                        $eduArr = explode('·', $values[$value + 2]);
                        $eduArr[0] && $education = trim($eduArr[0]);
                    }else{
                        $eduArr[1] = $values[$value + 1];
                    }
                }
                $major = trim($eduArr[1]??'');

                $eduTime = $values[$value];
                $eduTime = explode('-', $eduTime);
                $startTime = str_replace('.', '-', trim($eduTime[0]??''));
                $endTime = str_replace('.', '-', trim($eduTime[1]??''));

                $education_list[] = [
                    "starttime" => $startTime,
                    'endtime' => $endTime,
                    'school' => $school,
                    'major' => $major,
                    'education' => $education
                ];
            }
            $data['education_list'] = $education_list??[];

        }

        //工作经历
        $workList = [];
        if($values = $groups['工作经历']??''){
            $regex_time_str = '/(\d{4}\.\d{2})\s+-\s+((\d{4}\.\d{2})|(至今)).*?(（\d+)?/';

            // 处理 ｜
            foreach($values as $k=>$value){// 删除带有 ｜ 且换行的数据
                if(preg_match('/^\(?([^u4e00-u9fa5]{6})\).*公司/',$value,$match)){//处理公司名称分开的数据
                    if(!empty($values[$k-1])){
                        if($values[$k-1] == '('){
                            $values[$k] = $values[$k-1].$value;
                            unset($values[$k-1]);
                            !empty($values[$k-2]) && $values[$k] = $values[$k-2].$values[$k];
                            unset($values[$k-2]);
                        }else{
                            $values[$k] = $values[$k-1].$value;
                            unset($values[$k-1]);
                        }
                    }
                    continue;
                }
                if(stripos($value,'｜') === false){
                    continue;
                }
                unset($values[$k]);

                if(empty($values[$k-1])){
                    if(!empty($values[$k-2]) && preg_match($regex_time_str,$values[$k-2])){
                        continue;
                    }
                }elseif(preg_match($regex_time_str,$values[$k-1])){
                    continue;
                }

                if(!empty($values[$k-1]) && preg_match('/((\d+(\.\d+)?)).*?（/',$values[$k-1],$matches)){// 处理工作时间分开的数据
                    $values[$k-2] .= $matches[1]??'';
                }
                unset($values[$k-1]);
            }
            $values = array_values($values);

            $start_v = preg_grep($regex_time_str,$values);
//            $start_v = preg_grep('//',$start_v);

            $start_arr = [];
            $end_arr = [];
            foreach ($start_v as $k=>$v){
                $start_arr[] = $k;
            }
            foreach ($start_arr as $k=>$v){
                if($k==0) continue;
               if(preg_match('/(\s+)|(公司)/',$values[$v - 2]) && !preg_match('/([,:])|(。)|(：)|(，)|(、)/',$values[$v - 2]) ){
                   if(stripos($values[$v-2],'公司') === false && stripos($values[$v-1],'公司') !== false){
                       $end_arr[] = $v-2;
                   }else{
                       $end_arr[] = $v-3;
                   }
                }else{
                   if(preg_match('/(\s+)|(公司)/',$values[$v - 1]) && !preg_match('/([,:])|(。)|(：)|(，)|(、)/',$values[$v - 1]) ){
                       $end_arr[] = $v - 2;
                   }else{
                       $end_arr[] = $v - 3;
                   }

                }

            }
            $end_arr[] = count($values);
//            var_export([$values,$start_arr,$end_arr]);
            foreach ($start_arr as $key=>$val){
                // 获取公司名称
                if($key == 0){
                    $first = $values[0]??'';
                }else{
                    $first = $values[($end_arr[$key-1]??0)+1]??'';
                    if(!preg_match('/(\s+)|(公司)+/',$first)){
                        $first = $values[($end_arr[$key-1]??0)+2]??'';
                        if (!preg_match('/(\s+)|(公司)+/', $first)) {
                            $first = $values[($end_arr[$key - 1] ?? 0) + 1] ?? '';
                        }
                    }
                }

                $first = explode(' ',trim($first));
                $company = $first[0]??'';
//                var_export([$first]);
                // 获取职位名称
                if(count($first) <= 1){// 公司名称和职位分开
                    $second = $values[$val -1];
                    if(stripos($second,' ') !== false){
                        $second = explode(' ',$second);
                        $job_name = !empty($second[1])?$second[1]:$second[0];
                        if(stripos($job_name,'公司') !== false){
                            $job_name = '其它';
                        }
                    }else{
                        $job_name = $second;
                    }
                }else{
                    $job_name = preg_replace('/(\D+).*?(（?)/','$1',end($first));
                }
                // 获取工作时间
                preg_match($regex_time_str, $values[$val], $matches);
                $startTime = str_replace('.', '-', $matches[1]??'');
                if(!empty($matches[2])){
                    if($matches[2] == '至今'){
                        $endTime = 0;
                        $todate = 1;
                    }else{
                        $endTime = str_replace('.', '-', $matches[2]);
                        $todate = 0;
                    }
                }

                // 获取职责
                $duty = '';
                if(!empty($end_arr[$key])){
                    for($i=$val+1;$i<=$end_arr[$key];$i++){
                        if(!empty($values[$i])){
                            if(preg_match('/(（((\d+个月)|(\d+年))）)|(\d+个月）)/',$values[$i])){
                                continue;
                            }
                            $duty .= ($values[$i])."\n";
                        }
                    }
                }
                $workList[] = [
                    'starttime' => $startTime??'',
                    'endtime' => $endTime??'',
                    'todate' => $todate??0,
                    'companyname' => $company??'',
                    'jobname' => $job_name??'',
                    'duty' => $duty??''
                ];
            }

        }
//        dd($workList);
        $data['work_list'] = $workList;

        //语言
        if ($values = $groups['/语言']??($groups['技能/语言']??'')) {
            $languageList = [];
            $langCount = count($values);
            $_values = [];
            for ($i = 0; $i < $langCount; $i += 1) {
                if (is_numeric($values[$i])) {
                    break;
                }
                if(stripos($values[$i],'声明：') !== false){
                    break;
                }
                if(stripos($values[$i],'Ready to work soon') !== false){
                    break;
                }
                if (strpos($values[$i], '终止服务') !== false) {
                    break;
                }
                $_values[] = $values[$i];
            }
            $func = function($v=null)use($_values,&$func){
                $v || $v = $_values;
                $languageList = [];
                if(count($v) <= 2){
                    $languageList[] = [
                        'language' => $v[0]??'',
                        'level' => $v[1]??0
                    ];
                }else{
                    if(count($v) >= 4){
                        $_v = array_values(array_slice($v,0,4));
                        for ($i = 0; $i < 2; $i += 1) {
                            $languageList[] = [
                                'language' => $_v[$i],
                                'level' => $_v[$i+2]??0
                            ];
                        }

                        count($v) > 4 && $languageList = array_merge($languageList,call_user_func($func,array_slice($v,4)));
                    }else{

                        $languageList = array_merge($languageList,call_user_func($func,array_slice($v,0,2)));
                    }
                }
                return $languageList;
            };
            $languageList = call_user_func($func);

            $data['language_list'] = $languageList;
        }

        //证书
        if ($value = $groups['证书']??'') {

            $certificate_list = [];
            $langCount = count($value);
            for ($i = 1; $i < $langCount; $i += 2) {
                if (!is_numeric($value[$i])) {
                    break;
                }
                if(stripos($value[$i],'声明：') !== false){
                    break;
                }
                if(stripos($value[$i],'Ready to work soon') !== false){
                    break;
                }
                if (strpos($value[$i], '终止服务') !== false) {
                    break;
                }
                $certificate_list[] = [
                    'name' => $value[$i-1],
                    'obtaintime' => $value[$i]
                ];
            }
            $data['certificate_list'] = $certificate_list;

        }
        return $data;
    }
    //前程无忧pdf解析
    static function qianchengPdfParse($arr) {
//        return [];
        $arr = self::qianchengPdfExtraHandleData($arr);

        $groupNames = ['基本信息', '求职意向', '个人优势', '工作经历','项目经验', '教育经历','在校情况', '技能/语言', '培训经历','证书','操作时间'];
        $groups = self::getGroupData($arr, $groupNames);
        return self::qianchengPdfHandleData($groups);
    }

    //前程无忧word解析
    static function qianchengWordParse($arr) {
        $groupNames = ['基本信息', '求职意向', '个人优势', '工作经历','项目经验', '教育经历','在校情况', '技能/语言', '培训经历','证书','操作时间'];
        $groups = self::getGroupData($arr, $groupNames);

        $data = [
            'basic' => [
                'fullname' => '',
                'sex' => 0,
                'birthday' => '',
                'residence' => '',
                'height' => '',
                'marriage' => '',
                'education' => '',
                'enter_job_time' => '',
                'householdaddress' => '',
                'specialty' => '',
                'addtime' => '',
                'platform' => '前程无忧',
                'contact' => [
                    'mobile' => '',
                    'email' => ''
                ],
                'download_time' => !empty($groups['操作时间'])?$groups['操作时间']:''
            ],
            'intention_list' => [],
            'education_list' => [],
            'work_list' => [],
            'language_list' => [],
            'certificate_list' => []
        ];

        //基本信息
        if (isset($groups['基本信息'])) {
            //手机号
            foreach ($groups['基本信息'] as $item) {
                preg_match('/[0-9]{11}/', $item, $matches);
                if (isset($matches[0])) {
                    $data['basic']['contact']['mobile'] = $matches[0];
                }
            }
            if (empty($data['basic']['contact']['mobile'])) {
                return return_data(500,'手机号为空');
            }

            $data['basic']['fullname'] = $groups['基本信息'][0];
            $baseArr = explode('｜', $groups['基本信息'][2]);
            $baseArr = array_map('trim', $baseArr);

            $data['basic']['sex'] = $baseArr[0] == '男' ? 1 : 2;

            preg_match('/[0-9]+/', $baseArr[1], $matches);
            $age = $matches[0];
            $data['basic']['birthday'] = date('Y', strtotime("-$age years"));

            $data['basic']['residence'] = $baseArr[2];

            preg_match('/[0-9]+/', $baseArr[3], $matches);
            if (isset($matches[0])) {
                if (strpos($baseArr[3], '年')) {
                    $year = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$year years"));
                } elseif (strpos($baseArr[3], '月')) {
                    $month = $matches[0];
                    $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$month month"));
                }
            }

            $eduIndex = array_search('最高学历/学位', $groups['基本信息']);
            $eduArr = explode('｜', $groups['基本信息'][$eduIndex + 1]);
//            $data['basic']['education'] = trim($eduArr[2]);
            if(!empty($eduArr[2])){
                $data['basic']['education'] = trim($eduArr[2]);
            }elseif(!empty($eduArr[1])){
                $data['basic']['education'] = trim($eduArr[1]);
            }else{
                $data['basic']['education'] = trim($eduArr[0]);
            }
        }

        //自我评价
        if (isset($groups['个人优势'])) {
            $data['basic']['specialty'] = implode("\n", $groups['个人优势']);
        }

        //求职意向
        if (isset($groups['求职意向'])) {
            $values = $groups['求职意向'];
            foreach ($values as $key=>$value){
                if(stripos($value,'｜') === false){
                    continue;
                }
                $job = trim($value);
                $job = explode('｜', $job);
                $category = trim($job[0]);
                $district = trim($job[2]);
                $district = str_replace('-', '/', $district);

                preg_match_all('/[0-9]+([.]{1}[0-9]+){0,1}/', $job[1], $matches);
                $minwage = $matches[0][0];
                $maxwage = $matches[0][1];

                $nature = $job[3];
                $trade = '';
                if (isset($values[$key + 1]) && stripos($values[$key + 1],'｜') === false) {
                    $trade = $values[$key + 1] == '不限行业' ? '' : $values[$key + 1];
                }
                $data['intention_list'][] = [
                    'nature' => $nature,
                    'category' => $category,
                    'district' => $district,
                    'minwage' => $minwage,
                    'maxwage' => $maxwage,
                    'trade' => $trade
                ];
            }
//            $job = trim($groups['求职意向'][0]);
//            $job = explode('｜', $job);
//            $category = trim($job[0]);
//            $district = trim($job[2]);
//            $district = str_replace('-', '/', $district);
//
//            preg_match_all('/[0-9]+([.]{1}[0-9]+){0,1}/', $job[1], $matches);
//            $minwage = $matches[0][0];
//            $maxwage = $matches[0][1];
//
//            $nature = $job[3];
//            $trade = '';
//            if (isset($groups['求职意向'][1])) {
//                $trade = $groups['求职意向'][1] == '不限行业' ? '' : $groups['求职意向'][1];
//            }
//
//            $data['intention_list'] = [
//                [
//                    'nature' => $nature,
//                    'category' => $category,
//                    'district' => $district,
//                    'minwage' => $minwage,
//                    'maxwage' => $maxwage,
//                    'trade' => $trade
//                ]
//            ];
        }

        //教育经历
        if ($values = $groups['教育经历']??[]) {
            $school = $values[0];
            if(preg_match('/(\d{4}\.\d{2}).*-.*(\d{4}\.\d{2}).*/',$school)){// 处理未填写学校名称的数据
                $school = '未填写';
            }


            $stop_arr = array_keys(preg_grep('/(声明：)/',$values));
            $end_key = $stop_arr?end($stop_arr) - 1:count($values);
            if($end_key < 2){
                $eduArr = explode('｜', $values[1]);
            }else{
                $eduArr = explode('｜', $values[2]);
                if (count($eduArr) <= 1 && !empty($values[3]) && stripos($values[3],'声明：') === false) {
                    $eduArr = explode('｜', $values[3]);
                }
            }
            if(!empty($eduArr[1])){
                $major = trim($eduArr[1]);
            }else{
                $major = trim($eduArr[0]);
            }
            $eduTime = current(preg_grep('/(\d{4}\.\d{2}).*-.*(\d{4}\.\d{2}).*/',$values));

//            $eduTime = $groups['教育经历'][1];dd($groups['教育经历'],$eduTime);
            $eduTime = explode('-', $eduTime);
            $startTime = str_replace('.', '-', trim($eduTime[0]));
            $endTime = str_replace('.', '-', trim($eduTime[1]));
            $education = trim($eduArr[0]);

            $data['education_list'] = [
                [
                    "starttime" => $startTime,
                    'endtime' => $endTime,
                    'school' => $school,
                    'major' => $major,
                    'education' => $education
                ]
            ];
        }

        //工作经历
        $workList = [];
        $starts = [];
        if($values = $groups['工作经历']??[]){
            foreach ($values as $index => $value) {
                if (strpos($value, '职位：') === 0) {
                    if ($index > 3) {
                        if(preg_match('/((\d+)\.(\d+)).*-.*(((\d+)\.(\d+))|(至今)).*?（+/',$values[$index-1])){
                            $starts[] = $index - 2;
                        }else{
                            $starts[] = $index - 3;
                        }
                    } else {
                        $starts[] = 0;
                    }
                }
            }
            foreach ($starts as $index => $start) {
                $end = $starts[$index + 1] ?? count($values);
                $info = array_slice($values, $start, $end - $start);

                preg_match('/(.*) - (((\d+)\.(\d+))|(至今)).*(（)?.*/', $info[1], $matches);

                $startTime = str_replace('.', '-', $matches[1]);
                $endTime = str_replace('.', '-', $matches[2]);
                $todate = 0;
                if ($endTime == '至今') {
                    $endTime = '';
                    $todate = 1;
                }
                $job_arr = preg_grep('/职位：.*/',$info);
                $jobName = trim(current($job_arr));
                $jobName = explode('：', $jobName)[1];
                $duty = implode("\n", array_slice($info, array_keys($job_arr)[0]+1));
                $workList[] = [
                    'starttime' => $startTime,
                    'endtime' => $endTime,
                    'todate' => $todate,
                    'companyname' => $info[0],
                    'jobname' => $jobName,
                    'duty' => $duty
                ];

            }
        }

        $data['work_list'] = $workList;

        //语言
        if (isset($groups['技能/语言'])) {
            $languageList = [];
            $langCount = count($groups['技能/语言']);
            for ($i = 0; $i < $langCount; $i += 2) {
                if (is_numeric($groups['技能/语言'][$i])) {
                    break;
                }
                if (strpos($groups['技能/语言'][$i], '声明') === 0) {
                    break;
                }
                $languageList[] = [
                    'language' => $groups['技能/语言'][$i],
                    'level' => $groups['技能/语言'][$i + 1]
                ];
            }
            $data['language_list'] = $languageList;
        }
        //语言
        if (isset($groups['证书'])) {
            $value = $groups['证书'];
            $languageList = [];
            $langCount = count($value);
            for ($i = 1; $i < $langCount; $i += 2) {
                if (!is_numeric($value[$i])) {
                    break;
                }
                if (strpos($value[$i], '声明') === 0) {
                    break;
                }
                $languageList[] = [
                    'name' => $value[$i - 1],
                    'obtaintime' => $value[$i]
                ];
            }
            $data['certificate_list'] = $languageList;

        }
        return $data;
    }

    //58同城简历解析
    static function tongchengHtmlParse($arr) {
        $groupNames = ['基本信息', '求职意向', '工作经验', '教育经历'];
        $groups = self::getGroupData($arr, $groupNames);

        $data = [
            'basic' => [
                'fullname' => '',
                'sex' => 0,
                'birthday' => '',
                'residence' => '',
                'height' => '',
                'marriage' => '',
                'education' => '',
                'enter_job_time' => '',
                'householdaddress' => '',
                'specialty' => '',
                'addtime' => '',
                'platform' => '58同城',
                'contact' => [
                    'mobile' => '',
                    'email' => ''
                ],
                'download_time' => ''
            ],
            'intention_list' => [],
            'education_list' => [],
            'work_list' => [],
            'language_list' => [],
            'certificate_list' => []
        ];

        if (isset($groups['基本信息'])) {
            foreach ($groups['基本信息'] as $item) {

                preg_match('/.*更新时间：(.*)/', $item, $matches);
                if (isset($matches[1])) {
                    $data['basic']['download_time'] = trim($matches[1]);
                }

                preg_match('/(.*)\（(.*)，(.*)岁\）/', $item, $matches);
                if (count($matches) == 4) {
                    $data['basic']['fullname'] = $matches[1];
                    $data['basic']['sex'] = $matches[2] == '男' ? 1 : 2;
                    $age = intval($matches[3]);
                    if ($age) {
                        $data['basic']['birthday'] = date('Y-01-01', strtotime("-$age years"));
                    }
                }

                if (strpos($item, '现居住')) {
                    $eduArr = explode(' ', $item);
                    $data['basic']['education'] = trim($eduArr[0]);
                }

                if (strpos($item, '手机号码') !== false) {
                    $data['basic']['contact']['mobile'] = explode('：', $item)[1];
                }
            }
        }

        //求职意向
        if (isset($groups['求职意向'])) {
            $job = trim($groups['求职意向'][0]);
            $job = explode(' ', $job);
            $category = $job[1] ?? '';
            $district = $job[5] ?? '';

            preg_match_all('/[0-9]+([.]{1}[0-9]+){0,1}/', $job[3] ?? '', $matches);
            $minwage = $matches[0][0] ?? '';
            $maxwage = $matches[0][1] ?? '';

            $nature = '';
            $trade = '';
            if (isset($groups['基本信息'][4])) {
                $trade = $groups['基本信息'][4] == '不限行业' ? '' : $groups['基本信息'][4];
            }

            $data['intention_list'] = [
                [
                    'nature' => $nature,
                    'category' => $category,
                    'district' => $district,
                    'minwage' => $minwage,
                    'maxwage' => $maxwage,
                    'trade' => $trade
                ]
            ];
        }

        //教育经历
        if (isset($groups['教育经历'])) {
            $eduArr = explode(' ', $groups['教育经历'][0]);
            $school = $eduArr[1] ?? '';
            $major = $eduArr[2] ?? '';

            preg_match('/(.*)年(.*)月/', $eduArr[0] ?? '', $matches);
            $startTime = '';
            $endTime = isset($matches[2]) ? $matches[1] . '-' . $matches[2] : '';

            $data['education_list'] = [
                [
                    "starttime" => $startTime,
                    'endtime' => $endTime,
                    'school' => $school,
                    'major' => $major,
                    'education' => ''
                ]
            ];
        }

        //工作经历
        $workList = [];
        $starts = [];
        if(!empty($groups['工作经验'])){
            foreach ($groups['工作经验'] as $index => $value) {
                if (strpos($value, '工作内容：') === 0) {
                    $starts[] = $index - 2;
                }
            }
            foreach ($starts as $index => $start) {
                $end = $starts[$index + 1] ?? count($groups['工作经验']);
                $info = array_slice($groups['工作经验'], $start, $end - $start);

                preg_match('/(.*)-(.*) /', $info[1], $matches);
                $startTime = '';
                if (isset($matches[1])) {
                    $startTime = str_replace(['年', '月'], ['-', ''], $matches[1]);
                }
                $endTime = '';
                if (isset($matches[2])) {
                    $endTime = str_replace(['年', '月'], ['-', ''], $matches[2]);
                }
                $todate = 0;
                if ($endTime == '至今') {
                    $endTime = '';
                    $todate = 1;
                }

                $jobName = explode(' ', $info[1])[1];
                $duty = explode('：', $info[2])[1];
                $workList[] = [
                    'starttime' => $startTime,
                    'endtime' => $endTime,
                    'todate' => $todate,
                    'companyname' => $info[0],
                    'jobname' => $jobName,
                    'duty' => $duty
                ];
            }
        }
        $data['work_list'] = $workList;

        return $data;
    }

    //boss直聘简历解析
    static function bossHtmlParse($arr) {
        $data = [
            'basic' => [
                'fullname' => '',
                'sex' => 0,
                'birthday' => '',
                'residence' => '',
                'height' => '',
                'marriage' => '',
                'education' => '',
                'enter_job_time' => '',
                'householdaddress' => '',
                'specialty' => '',
                'addtime' => '',
                'platform' => 'boss直聘',
                'contact' => [
                    'mobile' => '',
                    'email' => ''
                ],
                'download_time' => ''
            ],
            'intention_list' => [],
            'education_list' => [],
            'work_list' => [],
            'language_list' => [],
            'certificate_list' => [],
        ];

        foreach ($arr as $index => $item) {
            if (strpos($item, '候选人给你发了一封简历。')) {

                $category = explode(' ', $item)[1] ?? '';

                $data['basic']['fullname'] = $arr[$index + 1] ?? '';

                if (isset($arr[$index + 2])) {
                    $data['basic']['sex'] = $arr[$index + 2] == '男' ? 1 : 2;
                }

                if (isset($arr[$index + 3])) {
                    preg_match('/[0-9]+/', $arr[$index + 3], $matches);
                    if (isset($matches[0])) {
                        $age = $matches[0];
                        $data['basic']['birthday'] = date('Y', strtotime("-$age years"));
                    }
                }

                $minwage = '';
                $maxwage = '';
                if (isset($arr[$index + 7])) {
                    preg_match_all('/[0-9]+([.]{1}[0-9]+){0,1}/', $arr[$index + 7], $matches);
                    if (isset($matches[0][0])) {
                        $minwage = floatval($matches[0][0]) * 1000;
                    }
                    if (isset($matches[0][1])) {
                        $maxwage = floatval($matches[0][1]) * 1000;
                    }
                }

                if (isset($arr[$index + 4])) {
                    $data['intention_list'] = [
                        [
                            'nature' => 0,
                            'category' => $category,
                            'district' => $arr[$index + 4],
                            'minwage' => $minwage,
                            'maxwage' => $maxwage,
                            'trade' => 0
                        ]
                    ];
                }

                if (isset($arr[$index + 5])) {
                    $data['basic']['education'] = $arr[$index + 5];
                }

                if (isset($arr[$index + 6])) {
                    preg_match('/[0-9]+/', $arr[$index + 6], $matches);
                    if (isset($matches[0])) {
                        if (strpos($arr[$index + 6], '年')) {
                            $year = $matches[0];
                            $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$year years"));
                        } elseif (strpos($arr[$index + 6], '月')) {
                            $month = $matches[0];
                            $data['basic']['enter_job_time'] = date('Y-01-01', strtotime("-$month month"));
                        }
                    }
                }

            }
        }
        return $data;
    }

    static function bossPdfParse($arr) {
        $data = [];
        foreach ($arr as $item) {
            preg_match('/[0-9]{11}/', $item, $matches);
            if (isset($matches[0])) {
                $data['mobile'] = $matches[0];
                break;
            }
        }
        return $data;
    }

    //谷歌插件数组转换
    static function convertArr($arr,$platform){
        $data = [];
        if(!empty($arr)){
            //基础信息
            $data['basic']= [
                'fullname' => isset($arr['name'])?$arr['name']:'', //姓名
                'sex' => (isset($arr['gender']) and $arr['gender']=='female' )?2:1, //性别
                'birthday' => isset($arr['birthday'])?$arr['birthday']:'',//出生日期
                'residence' => isset($arr['location'])?$arr['location']:'',//居住地
                'height' => isset($arr['height'])?$arr['height']:'',//身高
                'marriage' => isset($arr['marriage'])?$arr['marriage']:'',//婚姻,
                'education' => isset($arr['qualification'])?$arr['qualification']:'',//学历,
                'enter_job_time' => isset($arr['enter_job_time'])?$arr['enter_job_time']:'',//参加工作时间
                'householdaddress' => isset($arr['householdaddress'])?$arr['householdaddress']:'',//家庭住址,
                'specialty' => isset($arr['selfIntroduction'])?$arr['selfIntroduction']:'',//自我评价,
                'addtime' => time(),
                'platform' => $platform,//平台
                'contact' => [
                    'mobile' => isset($arr['phoneNumber'])?$arr['phoneNumber']:'',//手机号,
                    'email' => isset($arr['email'])?$arr['email']:'',//邮箱,
                ]
            ];
            //求职意向
            $data['intention_list']=[
                [
                'nature' => isset($arr['nature'])?$arr['nature']:'全职', //是否全职
                'category' => isset($arr['expectedPosition'])?$arr['expectedPosition']:'其他',//求职岗位
                'district' => isset($arr['expectedCity'])?$arr['expectedCity']:'其他',//求职地点
                'minwage' => isset($arr['minwage'])?$arr['minwage']:0,//薪资最大值, $arr['salaryExpectation'] 20-30k×13薪
                'maxwage' => isset($arr['maxwage'])?$arr['maxwage']:0,//薪资最小值,
                'trade' => isset($arr['expectedIndustry'])?$arr['expectedIndustry']:'',//求职行业 备注：需要程序处理
                ]
            ];
            //学历
            if(isset($arr['eduExperience'])){
                $temp = [];
                foreach ($arr['eduExperience'] as $k=>$v){
                    $temp[] = [
                        "starttime" => isset($v['startTime'])?$v['startTime']:'',//开始时间
                        'endtime' => isset($v['endTime'])?$v['endTime']:'',//结束时间
                        'school' => isset($v['school'])?$v['school']:'',//学校
                        'major' => isset($v['major'])?$v['major']:'',//专业,
                        'education' => isset($v['qualification'])?$v['qualification']:'',//学历
                    ];
                }
                $data['education_list']=$temp;
            }
            //工作精力
            if(isset($arr['workExperiences'])){
                $temp = [];
                foreach ($arr['workExperiences'] as $k=>$v){
                    $temp[] = [
                        "starttime" => isset($v['startTime'])?$v['startTime']:'',//开始时间
                        'endtime' => isset($v['endTime'])?$v['endTime']:'',//结束时间
                        'companyname' => isset($v['company'])?$v['company']:'',//公司名
                        'jobname' => isset($v['position'])?$v['position']:'',//工作名称,
                        'duty' => isset($v['detail'])?$v['detail']:'',//工作精力
                    ];
                }
                $data['work_list']=$temp;
            }
            //语言
            if(isset($arr['languageAbility'])){
                $temp = [];
                foreach ($arr['languageAbility'] as $k=>$v){
                    $temp[] = [
                        "language" => isset($v['startTime'])?$v['startTime']:'',//语言
                        'level' => isset($v['endTime'])?$v['endTime']:'熟练',//水平
                    ];
                }
                $data['language_list']=$temp;
            }
            //驾照
            if(isset($arr['certificate_list'])){
                $data['certificate_list'] = [
                    'name' => '',
                    'obtaintime' => ''
                ];
            }
        }
        return $data;
    }

}